document.onkeydown = function(e) {
    key_down_handle(e)
};

var USER_PRESS_NULL = 0;
var USER_PRESS_UP = 1;
var USER_PRESS_RIGHT = 2;
var USER_PRESS_DOWN = 3;
var USER_PRESS_LEFT = 4;

var g_usr_choice = USER_PRESS_NULL;
var g_usr_new_choice = USER_PRESS_NULL;
var g_score = 0;

var g_FoodLeftPos = 0;
var g_FoodTopPos = 0;

function setDefaultParams()
{
    var Snake = document.getElementById("ID_Snake");
    var Food = document.getElementById("ID_Food");
    setPos(Snake,100,20);
    setPos(Food,60,60);
}

begin_game();


function begin_game() {

    var myVar = setInterval(myTimer, 100);
}
var getStyle = function(e, styleName) {
    var styleValue = "";
    if (document.defaultView && document.defaultView.getComputedStyle) {
        styleValue = document.defaultView.getComputedStyle(e, "").getPropertyValue(styleName);
    } else if (e.currentStyle) {
        styleName = styleName.replace(/\-(\w)/g, function(strMatch, p1) {
            return p1.toUpperCase();
        });
        styleValue = e.currentStyle[styleName];
    }
    return styleValue;
}

function setPos(obj, leftPosInt, topPosInt) {
    obj.style.left = leftPosInt.toString() + 'px';
    obj.style.top = topPosInt.toString() + 'px';
}

function setLeftPos(obj,leftPosInt)
{
    obj.style.left = leftPosInt.toString() + 'px';
}

function setTopPos(obj,topPosInt)
{
    obj.style.top = topPosInt.toString() + 'px';
}

function get_leftPos_parseInt(e) {
    var pos = e.getBoundingClientRect().left;
    return parseInt(pos);
}

function get_rightPos_parseInt(e) {
    var pos = e.getBoundingClientRect().right;
    return parseInt(pos);
}

function get_topPos_parseInt(e) {
    var pos = e.getBoundingClientRect().top;
    return parseInt(pos);
}

function get_BottomPos_parseInt(e) {
    var pos = e.getBoundingClientRect().bottom;
    return parseInt(pos);
}

function pxString_to_num(px_val) {
    var end_index = px_val.indexOf("px");
    var ret_string = px_val.subString(0, end_index);
    return parseInt(ret_string);
}

function pause_game_handle()
{
        alert("game_pause");
        g_usr_choice = USER_PRESS_NULL;
        g_usr_new_choice = USER_PRESS_NULL;
}

function key_down_handle(e) {
    switch (e.keyCode) {
        case 32:
                pause_game_handle();
                break;        
        //left
        case 37:
            if ( g_usr_choice == USER_PRESS_NULL 
                || g_usr_choice == USER_PRESS_DOWN 
                || g_usr_choice == USER_PRESS_UP)
            {
                 g_usr_new_choice = USER_PRESS_LEFT;
            }

            break;
        //up
        case 38:
            if ( g_usr_choice == USER_PRESS_NULL 
                || g_usr_choice == USER_PRESS_LEFT 
                || g_usr_choice == USER_PRESS_RIGHT)
            {
                 g_usr_new_choice = USER_PRESS_UP;
            }
            break;
        //right
        case 39:
            if ( g_usr_choice == USER_PRESS_NULL 
                || g_usr_choice == USER_PRESS_DOWN 
                || g_usr_choice == USER_PRESS_UP)
            {
                 g_usr_new_choice = USER_PRESS_RIGHT;
            }
            break;
        //down
        case 40:
            if ( g_usr_choice == USER_PRESS_NULL 
                || g_usr_choice == USER_PRESS_LEFT
                || g_usr_choice == USER_PRESS_RIGHT)
            {
                 g_usr_new_choice = USER_PRESS_DOWN;
            }
            break;
    }
};

function game_over_handle()
{
    alert("game_over");
    g_usr_choice = USER_PRESS_NULL;
    g_usr_new_choice = USER_PRESS_NULL;
    g_score = 0;
    setDefaultParams();
}

function check_Food_Pos()
{
    var bite_Food = 0;

    var Snake = document.getElementById("ID_Snake");
    var snakeLeftPos = get_leftPos_parseInt(Snake);
    var snakeTopPos = get_topPos_parseInt(Snake);
    var snakeRightPos = get_rightPos_parseInt(Snake);
    var snakeBottomPos = get_BottomPos_parseInt(Snake);

    var Food = document.getElementById("ID_Food");
    var foodLeftPos = get_leftPos_parseInt(Food);
    var foodTopPos = get_topPos_parseInt(Food);
    var foodRightPos = get_rightPos_parseInt(Food);
    var foodBottomPos = get_BottomPos_parseInt(Food);

    var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    
    // snake on the left of food
    if ( snakeRightPos >= foodLeftPos && snakeLeftPos <= foodRightPos)
    {
            if ( snakeBottomPos >= foodTopPos && snakeTopPos <= foodBottomPos)
            {
                bite_Food = 1;
            }
    }

    if ( bite_Food)
    {
        g_score += 1;
        g_FoodLeftPos = windowWidth*Math.random();
        g_FoodTopPos  =  windowHeight*Math.random();
        if ( g_FoodLeftPos + (foodRightPos - foodLeftPos) + 20>= windowWidth)
        {
            g_FoodLeftPos = g_FoodLeftPos - ( foodRightPos - foodLeftPos)
        }

        if ( g_FoodTopPos + (foodBottomPos - foodTopPos) + 20>= windowHeight)
        {
            g_FoodTopPos = g_FoodTopPos - ( foodBottomPos - foodTopPos);
        }
        setPos(Food,g_FoodLeftPos,g_FoodTopPos);
    }
}

function cal_Snake_Next_Pos()
{
    var game_is_over = 0;
    g_usr_choice = g_usr_new_choice;
    var snakeSpeed = 30;
    var Snake = document.getElementById("ID_Snake");
    var snakeLeftPos = get_leftPos_parseInt(Snake);
    var snakeTopPos = get_topPos_parseInt(Snake);
    var snakeRightPos = get_rightPos_parseInt(Snake);
    var snakeBottomPos = get_BottomPos_parseInt(Snake);

    var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

    if ( USER_PRESS_RIGHT == g_usr_choice)
    {
        if ( snakeLeftPos + snakeSpeed < windowWidth)
        {
            snakeLeftPos += snakeSpeed;
            setLeftPos(Snake,snakeLeftPos);
        }
        else
        {
            game_over_handle();
            game_is_over = 1;
        }
    }
    else if ( USER_PRESS_LEFT == g_usr_choice )
    {
        if ( snakeLeftPos <= snakeSpeed)
        {
            game_over_handle();
            game_is_over = 1;
        }
        else
        {
            snakeLeftPos -= snakeSpeed;
            setLeftPos(Snake,snakeLeftPos);
        }
    }
    else if ( USER_PRESS_UP == g_usr_choice)
    {
        if ( snakeTopPos > snakeSpeed)
        {
            snakeTopPos -= snakeSpeed;
            setTopPos(Snake,snakeTopPos);
        }   
        else
        {
            game_over_handle();
            game_is_over = 1;
        }
    }
    else if ( USER_PRESS_DOWN == g_usr_choice)
    {
        if ( snakeTopPos + snakeSpeed < windowHeight)
        {
            snakeTopPos += snakeSpeed;
            setTopPos(Snake,snakeTopPos);
        }  
        else
        {
            game_over_handle();
            game_is_over = 1;
        }
    }

    if ( game_is_over == 0)
    {

        check_Food_Pos();
    }
    /*
       document.getElementById("ID_TestOut").innerHTML =
        "left:" + snakeLeftPos.toString() + " right:" 
        + snakeRightPos.toString() 
        + "\n"+"windowWidth:"+windowWidth.toString()
        + "\n"+"windowHeight:"+ windowHeight.toString()
        + "\n" +" top:"+snakeTopPos.toString() 
        + "\n" +"score:"+g_score.toString()
       +"\n" + "FoodLeft:" + g_FoodLeftPos.toString()
       + "\n" + "FoodTop:" + g_FoodTopPos.toString();
    */
}

function myTimer() {
    cal_Snake_Next_Pos();
}